(ns acst.routes.activities
  (:use acst.config.error-config
        acst.config.success-config)
  (:require [acst.models.activities :as act]
            [acst.models.forms-constraint :as fc]
            [acst.models.entry-forms :as ef]
            [noir.session :as session]
            [acst.models.users :refer [login-require admin-require]]
            [acst.utils.core :refer :all]
            [clojure.data.json :as json]
            [compojure.core :refer :all]))



(defn get-activities [limit page]
  (let [[int_page] (parse-to-int page)]
    (json/write-str (act/find-all-activity limit int_page))))

(defn insert-activity! [deadline title content]             ;;新建活动
  (admin-require
    (->
      (act/insert-activity! (session/get "user_id") deadline title content)
      (json/write-str))))

(defn update-activity! [activity_id deadline title content]             ;;更新活动
  (admin-require
    (->
      (let [[int_activity_id] (parse-to-int activity_id)]
        (act/update-activity! (session/get "user_id") int_activity_id deadline title content))
      (json/write-str))))

(defn delete-activity! [activity_id]                        ;;删除活动
  (admin-require
    (->
      (let [[int_activity_id] (parse-to-int activity_id)]
        (act/delete-activity! int_activity_id (session/get "user_id")))
      (json/write-str))))

(defn get-form-constraint [activity_id]                     ;;获取报名表格式限制(regex字段存在xss风险,前端不可以直接解析)
  (->
    (let [[int_activity_id] (parse-to-int activity_id)]
      (fc/get-constraint int_activity_id))
    (json/write-str)))

(defn insert-form-constraint! [activity_id form_constraint] ;;新建报名表结构
  (admin-require
    (->
      (let [[int_activity_id] (parse-to-int activity_id)]
        (fc/insert-constraint! (session/get "user_id") int_activity_id form_constraint))
      (json/write-str))))

(defn update-form-constraint! [activity_id form_constraint]
  (admin-require
    (->
      (let [[int_activity_id] (parse-to-int activity_id)]
        (fc/update-constraint! (session/get "user_id") int_activity_id form_constraint))
      (json/write-str))))

(defn delete-form-constranint! [activity_id]
  (admin-require
    (->
      (let [[int_activity_id] (parse-to-int activity_id)]
        (fc/delete-constraint! int_activity_id (session/get "user_id")))
      (json/write-str))))

(defn get-activity-entry-form [activity_id]
  (admin-require
    (->
      (let [[int_activity_id] (parse-to-int activity_id)]
        (ef/find-entry-forms-by-activity-id int_activity_id))
      (json/write-str))))

(defn sign-up-activity! [activity_id value]
  (login-require
    (->
      (let [[int_activity_id] (parse-to-int activity_id)]
        (ef/insert-entry-form! (session/get "user_id") int_activity_id value))
      (json/write-str))))

(defroutes activities-routes
           (context "/api/act" []
             (GET    "/" [page] (handler get-activities nil 20 page 1)) ;;获取活动
             (POST   "/" [deadline title content] (handler insert-activity! deadline nil title nil content nil)) ;;添加活动
             (context "/:activity_id" []
               (PUT    "/" [activity_id deadline title content] (handler update-activity! activity_id nil deadline nil title nil content nil)) ;;更新活动
               (DELETE "/" [activity_id] (handler delete-activity! activity_id nil))
               (GET    "/constraint" [activity_id] (handler get-form-constraint activity_id nil)) ;;获取活动报名表格式限制
               (POST   "/constraint" [activity_id constraint] (handler insert-form-constraint! activity_id nil constraint nil))
               (PUT    "/constraint" [activity_id constraint] (handler update-form-constraint! activity_id nil constraint nil))
               (DELETE "/constraint" [activity_id] (handler delete-form-constranint! activity_id nil))
               (GET    "/signup" [activity_id] (handler get-activity-entry-form activity_id nil))
               (POST   "/signup" [activity_id value] (handler sign-up-activity! activity_id nil value nil)))))
